﻿import System.IO
import qualified System.IO.UTF8 as UTF8
import Data.Char (isSpace, toLower)
import Data.List

trim :: String -> String
trim = f . f
    where f = reverse . dropWhile isSpace

main :: IO ()
main = do
    dictFile <- UTF8.readFile "ran.txt"        
    UTF8.writeFile "dict.txt"  $ convert $  dictFile

convert :: String -> String
convert text = concat $ extractWords $ lines text

extractWords :: [String] -> [String]
extractWords (x:[]) = []
extractWords (x:y:xs) | '-' `elem` x = extractWords xs
                      | ' ' `elem` x = extractWords xs
                      | not $ ('`' `elem` y || '\'' `elem` y) = extractWords xs
                      | otherwise = [(map toLower $ takeWhile (/=',') $ trim x) ++ ":" ++ (getAccent(trim y) 1) ++ "\n"] ++ extractWords xs

        
getAccent :: String -> Int -> String
getAccent [] _ = []
getAccent (x:xs) pos | toLower x == '`' = show pos
                     | otherwise = getAccent xs (pos + 1)

                   
